import { useDataStore } from '@/store/data';
import {storeToRefs} from "pinia";
const dataStore=useDataStore();
const {username,sensorsOpen,menu,assessorCompany,areaLists,currencyRate,getMenu,allProduct}=storeToRefs(dataStore)
//节流
export function throttle(fn:Function,delay:number) {
    let timer:any;
    return () => {
        if(!timer){
            timer=setTimeout(()=>{
                // @ts-ignore
                fn.apply(this,arguments);
                clearTimeout(timer);
                timer=null
            },delay)
        }
   }
}
// 防抖
export function debounce(fun: Function, delay: number) {
    let timer: any;
    return function () {
        var args = arguments;
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            // @ts-ignore
            fun.apply(this, args);
        }, delay);
    }
}

export function timeFormat (dateStr) {
    var timeStr = new Date(dateStr);
    var year = timeStr.getFullYear();
    var month = (timeStr.getMonth() + 1) < 10 ? ("0" + (timeStr.getMonth() + 1)) : (timeStr.getMonth() + 1);
    var day = timeStr.getDate() < 10 ? ("0" + timeStr.getDate()) : timeStr.getDate();
    var hour = timeStr.getHours() < 10 ? ("0" + timeStr.getHours()) : timeStr.getHours();
    var minute = timeStr.getMinutes() < 10 ? ("0" + timeStr.getMinutes()) : timeStr.getMinutes();
    var second = timeStr.getSeconds() < 10 ? ("0" + timeStr.getSeconds()) : timeStr.getSeconds();
    return year + '-' + month + '-' + day + ' '+hour + ':' + minute + ':' + second
}
//获取合作状态
export function getAllProduct(code){
    var name = '';
    // @ts-ignore
    allProduct.value.allProduct.forEach(function(item){
        if(item.code ==  code){
            name =  item.name
        }
    })
    return name ;
}
//过滤分公司
export function getBranchCodeUtil(row){
    var name = '';
    // @ts-ignore
    menu.value.company.forEach(function(item){
        if(item.branchCode ==  row.branch||item.branchCode ==  row.branchCode||item.branchCode ==  row.caseBranch){
            name =  item.branchName
        }
    });

    return name

}
//过滤截止日期
export function getUpdateDateUtil(row){
    if(row.deadLine){
        return row.deadLine.split(' ')[0];
    }else{
        return ''
    }
}
//获取险种信息
export function getkindCode(row){
    var name = '';
    // @ts-ignore
    menu.value.productList.forEach(function(item){
        if(item.code ==  row.classCode||item.code ==  row.productCode){
            name =  item.name
        }
    })
    return name ;
}

export function getQuantityUnitUtil(row){
    var name = row.quantity + '-'+row.busiInfoSix;
    return name
}
//加*处理
export function plusXing (str,frontLen,endLen) {
    if(!str)return str

    if(str.length<=2){
        return str.substring(0,1) + new Array(str.length).join('*')
    }else{
        var len = str.length-frontLen-endLen;
        var xing = '';
        for (var i=0;i<len;i++) {
            xing+='*';
        }
        return str.substring(0,frontLen)+xing+str.substring(str.length-endLen);
    }
}
